package com.poisonh.unifiedauth.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.util.Log;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.lzy.okhttputils.model.HttpParams;

import org.json.JSONObject;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 封装HTTPS网络请求框架
 * Created by PoisonH on 2016/7/12.
 */
public class MyHttpsUtils
{
    private static MyHttpsUtils mInstance;
    private Handler mHandler;

    private MyHttpsUtils()
    {
        //表示放到主UI线程去处理
        mHandler = new Handler(Looper.getMainLooper());
    }

    /**
     * 获取MyHttpsUtils单例
     *
     * @return
     */
    public synchronized static MyHttpsUtils getInstance()
    {
        if (null == mInstance)
        {
            mInstance = new MyHttpsUtils();
        }
        return mInstance;
    }

    /**
     * 两个回调接口
     */
    public static abstract class ResultCallback
    {
        /**
         * 请求成功回调
         *
         * @param response
         */
        public abstract void onSuccess(String response);

        /**
         * 请求失败回调
         *
         * @param failReason 失败原因
         */
        public abstract void onFailure(String failReason);


    }

    private void sendFailCallback(final ResultCallback callback, final String reason)
    {
        mHandler.post(new Runnable()
        {
            @Override
            public void run()
            {
                if (callback != null)
                {
                    callback.onFailure(reason);
                }
            }
        });
    }

    private void sendSuccessCallBack(final ResultCallback callback, final String str)
    {
        mHandler.post(new Runnable()
        {
            @Override
            public void run()
            {
                if (callback != null)
                {
                    callback.onSuccess(str);
                }
            }
        });
    }

    private void postRequest(String url, HttpParams params, ResultCallback callback)
    {
        try
        {
            OkHttpUtils.post(url).params(params).execute(new HttpsCallBack(callback));
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private class HttpsCallBack extends StringCallback
    {
        ResultCallback mCallback;

        public HttpsCallBack(ResultCallback callback)
        {
            this.mCallback = callback;
        }

        @Override
        public void onResponse(boolean b, String s, Request request, Response response)
        {
            Log.d("LoginActivity", s);
            try
            {
                JSONObject mjson = new JSONObject(s);
                String code = mjson.getString("code");
                if (code.equals("200"))
                {
                    mInstance.sendSuccessCallBack(mCallback, s);
                } else if (code.equals("301"))
                {
                    mInstance.sendFailCallback(mCallback, "Token过期");
                } else
                {
                    mInstance.sendFailCallback(mCallback, mjson.getString("msg"));
                }
            } catch (Exception e)
            {
                e.printStackTrace();
              //  Log.e("Tag",e.toString());
            }
        }

        @Override
        public void onError(boolean isFromCache, Call call, Response response, @Nullable Exception e)
        {
            super.onError(isFromCache, call, response, e);
            mInstance.sendFailCallback(mCallback, "服务器连接失败");
        }
    }

    /**
     * 外部post请求接口
     *
     * @param url
     * @param params
     * @param callback
     */
    public static void post(String url, HttpParams params, ResultCallback callback)
    {
        getInstance().postRequest(url, params, callback);
    }

}
